
#include <BLabel>
#include <BGridLayout>
#include <BVBoxLayout>
#include <BGroupBox>

#include "StringUtils.h"
#include "SixdofRestraintPanel.h"

SixdofRestraintPanel::SixdofRestraintPanel(BWidget* parent) : BWidget(parent)
{
	this->setTitle(BSixdofRestraint::TypeName());

	BGroupBox* pivotGroupBox = new BGroupBox();
	{
		_pivotAEdit = new BLineEdit();
		_pivotBEdit = new BLineEdit();
		BGridLayout* gridLayout = new BGridLayout(pivotGroupBox);
		gridLayout->addWidget(0, 0, new BLabel("pivot A"));
		gridLayout->addWidget(0, 1, _pivotAEdit);
		gridLayout->addWidget(1, 0, new BLabel("pivot B"));
		gridLayout->addWidget(1, 1, _pivotBEdit);
	}

	BGroupBox* linearLimitGroupBox = new BGroupBox("Linear Limit");
	{
		BLabel* lowerLabel = new BLabel("lower");
		_linearXMinSpinBox = new BRealSpinBox(-999, 999, 3);
		_linearYMinSpinBox = new BRealSpinBox(-999, 999, 3);
		_linearZMinSpinBox = new BRealSpinBox(-999, 999, 3);

		BLabel* upperLabel = new BLabel("upper");
		_linearXMaxSpinBox = new BRealSpinBox(-999, 999, 3);
		_linearYMaxSpinBox = new BRealSpinBox(-999, 999, 3);
		_linearZMaxSpinBox = new BRealSpinBox(-999, 999, 3);

		BLabel* valueLabel = new BLabel("value");
		_linearXValueSpinBox = new BRealSpinBox(-999, 999, 3);
		_linearYValueSpinBox = new BRealSpinBox(-999, 999, 3);
		_linearZValueSpinBox = new BRealSpinBox(-999, 999, 3);

		BGridLayout* gridLayout = new BGridLayout(linearLimitGroupBox);
		gridLayout->addWidget(1, 0, lowerLabel);
		gridLayout->addWidget(1, 1, _linearXMinSpinBox);
		gridLayout->addWidget(1, 2, _linearYMinSpinBox);
		gridLayout->addWidget(1, 3, _linearZMinSpinBox);
		gridLayout->addWidget(2, 0, upperLabel);
		gridLayout->addWidget(2, 1, _linearXMaxSpinBox);
		gridLayout->addWidget(2, 2, _linearYMaxSpinBox);
		gridLayout->addWidget(2, 3, _linearZMaxSpinBox);
		gridLayout->addWidget(4, 0, valueLabel);
		gridLayout->addWidget(4, 1, _linearXValueSpinBox);
		gridLayout->addWidget(4, 2, _linearYValueSpinBox);
		gridLayout->addWidget(4, 3, _linearZValueSpinBox);

	}

	BGroupBox* angularLimitGroupBox = new BGroupBox("Angular Limit");
	{
		BLabel* lowerLabel = new BLabel("lower");
		_angularXMinSpinBox = new BRealSpinBox(-999, 999, 3);
		_angularYMinSpinBox = new BRealSpinBox(-999, 999, 3);
		_angularZMinSpinBox = new BRealSpinBox(-999, 999, 3);

		BLabel* upperLabel = new BLabel("upper");
		_angularXMaxSpinBox = new BRealSpinBox(-999, 999, 3);
		_angularYMaxSpinBox = new BRealSpinBox(-999, 999, 3);
		_angularZMaxSpinBox = new BRealSpinBox(-999, 999, 3);

		BLabel* valueLabel = new BLabel("value");
		_angularXValueSpinBox = new BRealSpinBox(-999, 999, 3);
		_angularYValueSpinBox = new BRealSpinBox(-999, 999, 3);
		_angularZValueSpinBox = new BRealSpinBox(-999, 999, 3);

		BGridLayout* gridLayout = new BGridLayout(angularLimitGroupBox);
		gridLayout->addWidget(1, 0, lowerLabel);
		gridLayout->addWidget(1, 1, _angularXMinSpinBox);
		gridLayout->addWidget(1, 2, _angularYMinSpinBox);
		gridLayout->addWidget(1, 3, _angularZMinSpinBox);
		gridLayout->addWidget(2, 0, upperLabel);
		gridLayout->addWidget(2, 1, _angularXMaxSpinBox);
		gridLayout->addWidget(2, 2, _angularYMaxSpinBox);
		gridLayout->addWidget(2, 3, _angularZMaxSpinBox);
		gridLayout->addWidget(4, 0, valueLabel);
		gridLayout->addWidget(4, 1, _angularXValueSpinBox);
		gridLayout->addWidget(4, 2, _angularYValueSpinBox);
		gridLayout->addWidget(4, 3, _angularZValueSpinBox);
	}

	BGroupBox* linearMotorGroupBox = new BGroupBox("Linear Motor");
	{
		_linearMotorXCheckBox = new BCheckBox("enable x");
		_linearMotorYCheckBox = new BCheckBox("enable y");
		_linearMotorZCheckBox = new BCheckBox("enable z");

		_linearMotorXTargetSpinBox = new BFloatSpinBox(-99999, 99999, 3);
		_linearMotorYTargetSpinBox = new BFloatSpinBox(-99999, 99999, 3);
		_linearMotorZTargetSpinBox = new BFloatSpinBox(-99999, 99999, 3);
	
		_linearMotorXVelocitySpinBox = new BDoubleSpinBox(0, 99999, 3);
		_linearMotorYVelocitySpinBox = new BDoubleSpinBox(0, 99999, 3);
		_linearMotorZVelocitySpinBox = new BDoubleSpinBox(0, 99999, 3);
	
		_linearMotorXMaxForceSpinBox = new BDoubleSpinBox(0, 99999, 3);
		_linearMotorYMaxForceSpinBox = new BDoubleSpinBox(0, 99999, 3);
		_linearMotorZMaxForceSpinBox = new BDoubleSpinBox(0, 99999, 3);

		_linearMotorXTargetSlider = new BFloatSlider(-99999, 99999);
		_linearMotorYTargetSlider = new BFloatSlider(-99999, 99999);
		_linearMotorZTargetSlider = new BFloatSlider(-99999, 99999);

		BGridLayout* gridLayout = new BGridLayout(linearMotorGroupBox);
		gridLayout->addWidget(0, 0, new BLabel("Switch"));
		gridLayout->addWidget(0, 1, _linearMotorXCheckBox);
		gridLayout->addWidget(0, 2, _linearMotorYCheckBox);
		gridLayout->addWidget(0, 3, _linearMotorZCheckBox);

		gridLayout->addWidget(1, 0, new BLabel("Target"));
		gridLayout->addWidget(1, 1, _linearMotorXTargetSpinBox);
		gridLayout->addWidget(1, 2, _linearMotorYTargetSpinBox);
		gridLayout->addWidget(1, 3, _linearMotorZTargetSpinBox);

		gridLayout->addWidget(2, 0, new BLabel("Velocity"));
		gridLayout->addWidget(2, 1, _linearMotorXVelocitySpinBox);
		gridLayout->addWidget(2, 2, _linearMotorYVelocitySpinBox);
		gridLayout->addWidget(2, 3, _linearMotorZVelocitySpinBox);

		gridLayout->addWidget(3, 0, new BLabel("Max Force"));
		gridLayout->addWidget(3, 1, _linearMotorXMaxForceSpinBox);
		gridLayout->addWidget(3, 2, _linearMotorYMaxForceSpinBox);
		gridLayout->addWidget(3, 3, _linearMotorZMaxForceSpinBox);

		gridLayout->addWidget(4, 0, new BLabel("Slider"));
		gridLayout->addWidget(4, 1, _linearMotorXTargetSlider);
		gridLayout->addWidget(4, 2, _linearMotorYTargetSlider);
		gridLayout->addWidget(4, 3, _linearMotorZTargetSlider);

		gridLayout->setColumnPolicy(0, Policy_Preferred);
	}

	BGroupBox* angularMotorGroupBox = new BGroupBox("Angular Motor");
	{
		_angularMotorXCheckBox = new BCheckBox("enable x");
		_angularMotorYCheckBox = new BCheckBox("enable y");
		_angularMotorZCheckBox = new BCheckBox("enable z");

		_angularMotorXTargetSpinBox = new BFloatSpinBox(-99999, 99999, 3);
		_angularMotorYTargetSpinBox = new BFloatSpinBox(-99999, 99999, 3);
		_angularMotorZTargetSpinBox = new BFloatSpinBox(-99999, 99999, 3);
		
		_angularMotorXVelocitySpinBox = new BDoubleSpinBox(0, 99999, 3);
		_angularMotorYVelocitySpinBox = new BDoubleSpinBox(0, 99999, 3);
		_angularMotorZVelocitySpinBox = new BDoubleSpinBox(0, 99999, 3);

		_angularMotorXMaxForceSpinBox = new BDoubleSpinBox(0, 99999, 3);
		_angularMotorYMaxForceSpinBox = new BDoubleSpinBox(0, 99999, 3);
		_angularMotorZMaxForceSpinBox = new BDoubleSpinBox(0, 99999, 3);

		_angularMotorXTargetSlider = new BFloatSlider(-99999, 99999);
		_angularMotorYTargetSlider = new BFloatSlider(-99999, 99999);
		_angularMotorZTargetSlider = new BFloatSlider(-99999, 99999);
	
		BGridLayout* gridLayout = new BGridLayout(angularMotorGroupBox);
		gridLayout->addWidget(0, 0, new BLabel("Switch"));
		gridLayout->addWidget(0, 1, _angularMotorXCheckBox);
		gridLayout->addWidget(0, 2, _angularMotorYCheckBox);
		gridLayout->addWidget(0, 3, _angularMotorZCheckBox);

		gridLayout->addWidget(1, 0, new BLabel("Target"));
		gridLayout->addWidget(1, 1, _angularMotorXTargetSpinBox);
		gridLayout->addWidget(1, 2, _angularMotorYTargetSpinBox);
		gridLayout->addWidget(1, 3, _angularMotorZTargetSpinBox);

		gridLayout->addWidget(2, 0, new BLabel("Velocity"));
		gridLayout->addWidget(2, 1, _angularMotorXVelocitySpinBox);
		gridLayout->addWidget(2, 2, _angularMotorYVelocitySpinBox);
		gridLayout->addWidget(2, 3, _angularMotorZVelocitySpinBox);

		gridLayout->addWidget(3, 0, new BLabel("Max Force"));
		gridLayout->addWidget(3, 1, _angularMotorXMaxForceSpinBox);
		gridLayout->addWidget(3, 2, _angularMotorYMaxForceSpinBox);
		gridLayout->addWidget(3, 3, _angularMotorZMaxForceSpinBox);

		gridLayout->addWidget(4, 0, new BLabel("Slider"));
		gridLayout->addWidget(4, 1, _angularMotorXTargetSlider);
		gridLayout->addWidget(4, 2, _angularMotorYTargetSlider);
		gridLayout->addWidget(4, 3, _angularMotorZTargetSlider);

		gridLayout->setColumnPolicy(0, Policy_Preferred);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(pivotGroupBox);
	vlayout->addWidget(linearLimitGroupBox);
	vlayout->addWidget(angularLimitGroupBox);
	vlayout->addWidget(linearMotorGroupBox);
	vlayout->addWidget(angularMotorGroupBox);

	connect(this, Signal_AssetChanged, &SixdofRestraintPanel::slotAssetChanged);

	connect(_pivotAEdit, Signal_EditFinished, &SixdofRestraintPanel::slotPivotAEditFinished);
	connect(_pivotBEdit, Signal_EditFinished, &SixdofRestraintPanel::slotPivotBEditFinished);

	connect(_linearXMinSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotLinearLowerLimitEditFinished);
	connect(_linearYMinSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotLinearLowerLimitEditFinished);
	connect(_linearZMinSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotLinearLowerLimitEditFinished);

	connect(_linearXMaxSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotLinearUpperLimitEditFinished);
	connect(_linearYMaxSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotLinearUpperLimitEditFinished);
	connect(_linearZMaxSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotLinearUpperLimitEditFinished);

	connect(_angularXMinSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotAngularLowerLimitEditFinished);
	connect(_angularYMinSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotAngularLowerLimitEditFinished);
	connect(_angularZMinSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotAngularLowerLimitEditFinished);

	connect(_angularXMaxSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotAngularUpperLimitEditFinished);
	connect(_angularYMaxSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotAngularUpperLimitEditFinished);
	connect(_angularZMaxSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotAngularUpperLimitEditFinished);

	connect(_linearMotorXCheckBox, Signal_Toggled, &SixdofRestraintPanel::slotMotorEnabledBoxToggled);
	connect(_linearMotorYCheckBox, Signal_Toggled, &SixdofRestraintPanel::slotMotorEnabledBoxToggled);
	connect(_linearMotorZCheckBox, Signal_Toggled, &SixdofRestraintPanel::slotMotorEnabledBoxToggled);

	connect(_linearMotorXTargetSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorTargetEditFinished);
	connect(_linearMotorYTargetSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorTargetEditFinished);
	connect(_linearMotorZTargetSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorTargetEditFinished);

	connect(_linearMotorXVelocitySpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorVelocityEditFinished);
	connect(_linearMotorYVelocitySpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorVelocityEditFinished);
	connect(_linearMotorZVelocitySpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorVelocityEditFinished);

	connect(_linearMotorXMaxForceSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorMaxForceEditFinished);
	connect(_linearMotorYMaxForceSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorMaxForceEditFinished);
	connect(_linearMotorZMaxForceSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorMaxForceEditFinished);

	connect(_linearMotorXTargetSlider, Signal_Sliding, &SixdofRestraintPanel::slotMotorTargetSliding);
	connect(_linearMotorYTargetSlider, Signal_Sliding, &SixdofRestraintPanel::slotMotorTargetSliding);
	connect(_linearMotorZTargetSlider, Signal_Sliding, &SixdofRestraintPanel::slotMotorTargetSliding);

	connect(_angularMotorXCheckBox, Signal_Toggled, &SixdofRestraintPanel::slotMotorEnabledBoxToggled);
	connect(_angularMotorYCheckBox, Signal_Toggled, &SixdofRestraintPanel::slotMotorEnabledBoxToggled);
	connect(_angularMotorZCheckBox, Signal_Toggled, &SixdofRestraintPanel::slotMotorEnabledBoxToggled);

	connect(_angularMotorXTargetSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorTargetEditFinished);
	connect(_angularMotorYTargetSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorTargetEditFinished);
	connect(_angularMotorZTargetSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorTargetEditFinished);

	connect(_angularMotorXVelocitySpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorVelocityEditFinished);
	connect(_angularMotorYVelocitySpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorVelocityEditFinished);
	connect(_angularMotorZVelocitySpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorVelocityEditFinished);

	connect(_angularMotorXMaxForceSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorMaxForceEditFinished);
	connect(_angularMotorYMaxForceSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorMaxForceEditFinished);
	connect(_angularMotorZMaxForceSpinBox, Signal_EditFinished, &SixdofRestraintPanel::slotMotorMaxForceEditFinished);

	connect(_angularMotorXTargetSlider, Signal_Sliding, &SixdofRestraintPanel::slotMotorTargetSliding);
	connect(_angularMotorYTargetSlider, Signal_Sliding, &SixdofRestraintPanel::slotMotorTargetSliding);
	connect(_angularMotorZTargetSlider, Signal_Sliding, &SixdofRestraintPanel::slotMotorTargetSliding);

	connect(linearLimitGroupBox, Signal_DoubleClicked, &SixdofRestraintPanel::slotGroupBoxTitleDoubleClicked);
	connect(angularLimitGroupBox, Signal_DoubleClicked, &SixdofRestraintPanel::slotGroupBoxTitleDoubleClicked);
	connect(linearMotorGroupBox, Signal_DoubleClicked, &SixdofRestraintPanel::slotGroupBoxTitleDoubleClicked);
	connect(angularMotorGroupBox, Signal_DoubleClicked, &SixdofRestraintPanel::slotGroupBoxTitleDoubleClicked);

}
SixdofRestraintPanel::~SixdofRestraintPanel()
{

}

void SixdofRestraintPanel::slotAssetChanged(BObject* object, const BValue& value)
{
	_sixdofRestraint = value;
	if (_sixdofRestraint)
	{
		if (_sixdofRestraint->bodyA())
		{
			const BVector& pivotA = _sixdofRestraint->pivotA();
			//_bodyAGroupBox->setTitle(_sixdofRestraint->bodyA()->name());
			_pivotAEdit->setText(StringFromVector(pivotA));
			//_rotateAEdit->setText(StringFromQuater(matrixA.rotate()));
		}
		if (_sixdofRestraint->bodyB())
		{
			const BVector& pivotB = _sixdofRestraint->pivotB();
			//_bodyBGroupBox->setTitle(_sixdofRestraint->bodyB()->name());
			_pivotBEdit->setText(StringFromVector(pivotB));
			//_rotateBEdit->setText(StringFromQuater(matrixB.rotate()));
		}

		BVector linearLower = _sixdofRestraint->linearLowerLimit();
		_linearXMinSpinBox->setValue(linearLower.x());
		_linearYMinSpinBox->setValue(linearLower.y());
		_linearZMinSpinBox->setValue(linearLower.z());

		BVector linearUpper = _sixdofRestraint->linearUpperLimit();
		_linearXMaxSpinBox->setValue(linearUpper.x());
		_linearYMaxSpinBox->setValue(linearUpper.y());
		_linearZMaxSpinBox->setValue(linearUpper.z());

		BVector angularLower = _sixdofRestraint->angularLowerLimit();
		_angularXMinSpinBox->setValue(angularLower.x());
		_angularYMinSpinBox->setValue(angularLower.y());
		_angularZMinSpinBox->setValue(angularLower.z());

		BVector angularUpper = _sixdofRestraint->angularUpperLimit();
		_angularXMaxSpinBox->setValue(angularUpper.x());
		_angularYMaxSpinBox->setValue(angularUpper.y());
		_angularZMaxSpinBox->setValue(angularUpper.z());

		bool linearMotorEnabled_X = _sixdofRestraint->linearMotorEnabled(0);
		bool linearMotorEnabled_Y = _sixdofRestraint->linearMotorEnabled(1);
		bool linearMotorEnabled_Z = _sixdofRestraint->linearMotorEnabled(2);

		BReal linearMotorTarget_X = _sixdofRestraint->linearMotorTarget(0);
		BReal linearMotorTarget_Y = _sixdofRestraint->linearMotorTarget(1);
		BReal linearMotorTarget_Z = _sixdofRestraint->linearMotorTarget(2);

		BReal linearMotorVelocity_X = _sixdofRestraint->linearMotorVelocity(0);
		BReal linearMotorVelocity_Y = _sixdofRestraint->linearMotorVelocity(1);
		BReal linearMotorVelocity_Z = _sixdofRestraint->linearMotorVelocity(2);

		BReal linearMotorMaxForce_X = _sixdofRestraint->linearMotorMaxForce(0);
		BReal linearMotorMaxForce_Y = _sixdofRestraint->linearMotorMaxForce(1);
		BReal linearMotorMaxForce_Z = _sixdofRestraint->linearMotorMaxForce(2);

		_linearMotorXCheckBox->setChecked(linearMotorEnabled_X);
		_linearMotorYCheckBox->setChecked(linearMotorEnabled_Y);
		_linearMotorZCheckBox->setChecked(linearMotorEnabled_Z);

		_linearMotorXTargetSpinBox->setValue((float)linearMotorTarget_X);
		_linearMotorYTargetSpinBox->setValue((float)linearMotorTarget_Y);
		_linearMotorZTargetSpinBox->setValue((float)linearMotorTarget_Z);

		_linearMotorXVelocitySpinBox->setValue(linearMotorVelocity_X);
		_linearMotorYVelocitySpinBox->setValue(linearMotorVelocity_Y);
		_linearMotorZVelocitySpinBox->setValue(linearMotorVelocity_Z);

		_linearMotorXMaxForceSpinBox->setValue(linearMotorMaxForce_X);
		_linearMotorYMaxForceSpinBox->setValue(linearMotorMaxForce_Y);
		_linearMotorZMaxForceSpinBox->setValue(linearMotorMaxForce_Z);

		_linearMotorXTargetSlider->setRange((float)linearLower.x(), (float)linearUpper.x());
		_linearMotorYTargetSlider->setRange((float)linearLower.y(), (float)linearUpper.y());
		_linearMotorZTargetSlider->setRange((float)linearLower.z(), (float)linearUpper.z());

		_linearMotorXTargetSlider->setValue((float)linearMotorTarget_X);
		_linearMotorYTargetSlider->setValue((float)linearMotorTarget_Y);
		_linearMotorZTargetSlider->setValue((float)linearMotorTarget_Z);

		bool angularMotorEnabled_X = _sixdofRestraint->angularMotorEnabled(0);
		bool angularMotorEnabled_Y = _sixdofRestraint->angularMotorEnabled(1);
		bool angularMotorEnabled_Z = _sixdofRestraint->angularMotorEnabled(2);

		BReal angularMotorTarget_X = _sixdofRestraint->angularMotorTarget(0);
		BReal angularMotorTarget_Y = _sixdofRestraint->angularMotorTarget(1);
		BReal angularMotorTarget_Z = _sixdofRestraint->angularMotorTarget(2);

		BReal angularMotorVelocity_X = _sixdofRestraint->angularMotorVelocity(0);
		BReal angularMotorVelocity_Y = _sixdofRestraint->angularMotorVelocity(1);
		BReal angularMotorVelocity_Z = _sixdofRestraint->angularMotorVelocity(2);
	
		BReal angularMotorForce_X = _sixdofRestraint->angularMotorMaxForce(0);
		BReal angularMotorForce_Y = _sixdofRestraint->angularMotorMaxForce(1);
		BReal angularMotorForce_Z = _sixdofRestraint->angularMotorMaxForce(2);

		_angularMotorXCheckBox->setChecked(angularMotorEnabled_X);
		_angularMotorYCheckBox->setChecked(angularMotorEnabled_Y);
		_angularMotorZCheckBox->setChecked(angularMotorEnabled_Z);

		_angularMotorXTargetSpinBox->setValue((float)angularMotorTarget_X);
		_angularMotorYTargetSpinBox->setValue((float)angularMotorTarget_Y);
		_angularMotorZTargetSpinBox->setValue((float)angularMotorTarget_Z);
		
		_angularMotorXVelocitySpinBox->setValue(angularMotorVelocity_X);
		_angularMotorYVelocitySpinBox->setValue(angularMotorVelocity_Y);
		_angularMotorZVelocitySpinBox->setValue(angularMotorVelocity_Z);

		_angularMotorXMaxForceSpinBox->setValue(angularMotorForce_X);
		_angularMotorYMaxForceSpinBox->setValue(angularMotorForce_Y);
		_angularMotorZMaxForceSpinBox->setValue(angularMotorForce_Z);

		_angularMotorXTargetSlider->setRange((float)angularLower.x(), (float)angularUpper.x());
		_angularMotorYTargetSlider->setRange((float)angularLower.y(), (float)angularUpper.y());
		_angularMotorZTargetSlider->setRange((float)angularLower.z(), (float)angularUpper.z());

		_angularMotorXTargetSlider->setValue((float)angularMotorTarget_X);
		_angularMotorYTargetSlider->setValue((float)angularMotorTarget_Y);
		_angularMotorZTargetSlider->setValue((float)angularMotorTarget_Z);

	}
}

void SixdofRestraintPanel::slotPivotAEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	BVector pivotA = StringToVector(text);
	_sixdofRestraint->setPivotA(pivotA);
}
void SixdofRestraintPanel::slotPivotBEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	BVector pivotB = StringToVector(text);
	_sixdofRestraint->setPivotB(pivotB);
}

void SixdofRestraintPanel::slotLinearLowerLimitEditFinished(BObject* object, const BValue& value)
{
	BReal minx = _linearXMinSpinBox->value();
	BReal miny = _linearYMinSpinBox->value();
	BReal minz = _linearZMinSpinBox->value();
	_sixdofRestraint->setLinearLowerLimit(BVector(minx, miny, minz));
	_linearMotorXTargetSlider->setMinimum((float)minx);
	_linearMotorYTargetSlider->setMinimum((float)miny);
	_linearMotorZTargetSlider->setMinimum((float)minz);
}
void SixdofRestraintPanel::slotLinearUpperLimitEditFinished(BObject* object, const BValue& value)
{
	BReal maxx = _linearXMaxSpinBox->value();
	BReal maxy = _linearYMaxSpinBox->value();
	BReal maxz = _linearZMaxSpinBox->value();
	_sixdofRestraint->setLinearUpperLimit(maxx, maxy, maxz);
	_linearMotorXTargetSlider->setMaximum((float)maxx);
	_linearMotorYTargetSlider->setMaximum((float)maxy);
	_linearMotorZTargetSlider->setMaximum((float)maxz);
}

void SixdofRestraintPanel::slotAngularLowerLimitEditFinished(BObject* object, const BValue& value)
{
	BReal minx = _angularXMinSpinBox->value();
	BReal miny = _angularYMinSpinBox->value();
	BReal minz = _angularZMinSpinBox->value();
	_sixdofRestraint->setAngularLowerLimit(BVector(minx, miny, minz));
	_angularMotorXTargetSlider->setMinimum((float)minx);
	_angularMotorYTargetSlider->setMinimum((float)miny);
	_angularMotorZTargetSlider->setMinimum((float)minz);
}
void SixdofRestraintPanel::slotAngularUpperLimitEditFinished(BObject* object, const BValue& value)
{
	BReal maxx = _angularXMaxSpinBox->value();
	BReal maxy = _angularYMaxSpinBox->value();
	BReal maxz = _angularZMaxSpinBox->value();
	_sixdofRestraint->setAngularUpperLimit(BVector(maxx, maxy, maxz));
	_angularMotorXTargetSlider->setMaximum((float)maxx);
	_angularMotorYTargetSlider->setMaximum((float)maxy);
	_angularMotorZTargetSlider->setMaximum((float)maxz);
}

void SixdofRestraintPanel::slotMotorEnabledBoxToggled(BObject* object, const BValue& value)
{
	bool motorEnabled = value;
	if (object == _linearMotorXCheckBox)
	{
		_sixdofRestraint->setLinearMotorEnabled(0, motorEnabled);
	}
	else if (object == _linearMotorYCheckBox)
	{
		_sixdofRestraint->setLinearMotorEnabled(1, motorEnabled);
	}
	else if (object == _linearMotorZCheckBox)
	{
		_sixdofRestraint->setLinearMotorEnabled(2, motorEnabled);
	}
	else if (object == _angularMotorXCheckBox)
	{
		_sixdofRestraint->setAngularMotorEnabled(0, motorEnabled);
	}
	else if (object == _angularMotorYCheckBox)
	{
		_sixdofRestraint->setAngularMotorEnabled(1, motorEnabled);
	}
	else if (object == _angularMotorZCheckBox)
	{
		_sixdofRestraint->setAngularMotorEnabled(2, motorEnabled);
	}
	emit("object-changed", _sixdofRestraint);
}
void SixdofRestraintPanel::slotMotorTargetEditFinished(BObject* object, const BValue& value)
{
	float motorTarget = value;

	if (object == _linearMotorXTargetSpinBox)
		_sixdofRestraint->setLinearMotorTarget(0, motorTarget);
	if (object == _linearMotorYTargetSpinBox)
		_sixdofRestraint->setLinearMotorTarget(1, motorTarget);
	if (object == _linearMotorZTargetSpinBox)
		_sixdofRestraint->setLinearMotorTarget(2, motorTarget);

	if (object == _angularMotorXTargetSpinBox)
		_sixdofRestraint->setAngularMotorTarget(0, motorTarget);
	if (object == _angularMotorYTargetSpinBox)
		_sixdofRestraint->setAngularMotorTarget(1, motorTarget);
	if (object == _angularMotorZTargetSpinBox)
		_sixdofRestraint->setAngularMotorTarget(2, motorTarget);

	emit("object-changed", _sixdofRestraint);
}
void SixdofRestraintPanel::slotMotorVelocityEditFinished(BObject* object, const BValue& value)
{
	BReal motorVelocity = value;

	if (object == _linearMotorXVelocitySpinBox)
		_sixdofRestraint->setLinearMotorVelocity(0, motorVelocity);
	if (object == _linearMotorYVelocitySpinBox)
		_sixdofRestraint->setLinearMotorVelocity(1, motorVelocity);
	if (object == _linearMotorZVelocitySpinBox)
		_sixdofRestraint->setLinearMotorVelocity(2, motorVelocity);

	if (object == _angularMotorXVelocitySpinBox)
		_sixdofRestraint->setAngularMotorVelocity(0, motorVelocity);
	if (object == _angularMotorYVelocitySpinBox)
		_sixdofRestraint->setAngularMotorVelocity(1, motorVelocity);
	if (object == _angularMotorZVelocitySpinBox)
		_sixdofRestraint->setAngularMotorVelocity(2, motorVelocity);

	emit("object-changed", _sixdofRestraint);
}
void SixdofRestraintPanel::slotMotorMaxForceEditFinished(BObject* object, const BValue& value)
{
	BReal motorMaxForce = value;

	if (object == _linearMotorXMaxForceSpinBox)
		_sixdofRestraint->setLinearMotorMaxForce(0, motorMaxForce);
	if (object == _linearMotorYMaxForceSpinBox)
		_sixdofRestraint->setLinearMotorMaxForce(1, motorMaxForce);
	if (object == _linearMotorZMaxForceSpinBox)
		_sixdofRestraint->setLinearMotorMaxForce(2, motorMaxForce);

	if (object == _angularMotorXMaxForceSpinBox)
		_sixdofRestraint->setAngularMotorMaxForce(0, motorMaxForce);
	if (object == _angularMotorYMaxForceSpinBox)
		_sixdofRestraint->setAngularMotorMaxForce(1, motorMaxForce);
	if (object == _angularMotorZMaxForceSpinBox)
		_sixdofRestraint->setAngularMotorMaxForce(2, motorMaxForce);

	emit("object-changed", _sixdofRestraint);
}
void SixdofRestraintPanel::slotMotorTargetSliding(BObject* object, const BValue& value)
{
	float motorTarget = value;
	if (object == _linearMotorXTargetSlider)
	{
		_linearMotorXTargetSpinBox->setValue(motorTarget);
		_sixdofRestraint->setLinearMotorTarget(0, motorTarget);
	}
	else if (object == _linearMotorYTargetSlider)
	{
		_linearMotorYTargetSpinBox->setValue(motorTarget);
		_sixdofRestraint->setLinearMotorTarget(1, motorTarget);
	}
	else if (object == _linearMotorZTargetSlider)
	{
		_linearMotorZTargetSpinBox->setValue(motorTarget);
		_sixdofRestraint->setLinearMotorTarget(2, motorTarget);
	}
	else if (object == _angularMotorXTargetSlider)
	{
		_angularMotorXTargetSpinBox->setValue(motorTarget);
		_sixdofRestraint->setAngularMotorTarget(0, motorTarget);
	}
	else if (object == _angularMotorYTargetSlider)
	{
		_angularMotorYTargetSpinBox->setValue(motorTarget);
		_sixdofRestraint->setAngularMotorTarget(1, motorTarget);
	}
	else if (object == _angularMotorZTargetSlider)
	{
		_angularMotorZTargetSpinBox->setValue(motorTarget);
		_sixdofRestraint->setAngularMotorTarget(2, motorTarget);
	}
	emit("object-changed", _sixdofRestraint);
}

void SixdofRestraintPanel::slotGroupBoxTitleDoubleClicked(BObject* object, const BValue& value)
{
	BGroupBox* groupBox = (BGroupBox*)object;
	groupBox->setFolded(!groupBox->folded());
}

void SixdofRestraintPanel::updateEvent(const BEvent& event)
{
	if (_sixdofRestraint)
	{
		BVector offset = _sixdofRestraint->offset();
		_linearXValueSpinBox->setValue(offset.x());
		_linearYValueSpinBox->setValue(offset.y());
		_linearZValueSpinBox->setValue(offset.z());

		BVector angle = _sixdofRestraint->angle();
		_angularXValueSpinBox->setValue(bRTD(angle.x()));
		_angularYValueSpinBox->setValue(bRTD(angle.y()));
		_angularZValueSpinBox->setValue(bRTD(angle.z()));
	}
}
